﻿Imports System.Windows.Controls
Imports System.Windows

Public Class DataGridZone
    Inherits DashboardZone


    Dim _Visual As New DataGrid()
    Dim _DataProperty As DashboardZoneProperty
    Dim _GridProperty As DashboardZoneProperty

    Sub New()
        Me._Visual.CanUserAddRows = False
        Me._Visual.CanUserDeleteRows = False

        Me._DataProperty = New DashboardZoneProperty(Me, "Data", GetType(IEnumerable))
        Me._DataProperty.EnableDimensionOnBinding = True
        Me.Properties.Add(_DataProperty)

        AddHandler Me._Visual.AutoGeneratedColumns, AddressOf VisualAutoGeneratedColumns
        AddHandler Me._Visual.AutoGeneratingColumn, AddressOf VisualAutoGeneratingColumns
        'Me._Visual.Text = "(Modifier le texte)"
        Me._GridProperty = New DashboardZoneProperty(Me, Me, "Grid", "Grid")
        Me.Properties.Add(_GridProperty)
        'Me.Properties.Add(New DashboardZoneProperty(Me, "TextFormat", GetType(String)))
        'Me.Properties.Add(New DashboardZoneProperty(Me, "FontFamily"))
        'Me.Properties.Add(New DashboardZoneProperty(Me, "FontStyle"))
        'Me.Properties.Add(New DashboardZoneProperty(Me, "FontWeight"))
        'Me.Properties.Add(New DashboardZoneProperty(Me, "FontStretch"))
        'Me.Properties.Add(New DashboardZoneProperty(Me, "FontSize"))
        'Me.Properties.Add(New DashboardZoneProperty(Me, "Foreground"))
        'Me.Properties.Add(New DashboardZoneProperty(Me, "Background"))
        'Me.Properties.Add(New DashboardZoneProperty(Me, "TextDecorations"))
        'Me.Properties.Add(New DashboardZoneProperty(Me, "Padding"))
        'Me.Properties.Add(New DashboardZoneProperty(Me, "TextAlignment"))
        'Me.Properties.Add(New DashboardZoneProperty(Me, "TextTrimming"))
        'Me.Properties.Add(New DashboardZoneProperty(Me, "TextWrapping"))

    End Sub

    Public Property Grid As DataGrid
        Get
            Return _Visual
        End Get
        Set(value As DataGrid)
            _Visual = value
        End Set
    End Property

    Public Overrides ReadOnly Property Visual As UIElement
        Get
            Return _Visual
        End Get
    End Property

    Public Overrides ReadOnly Property DataProperty As DashboardZoneProperty
        Get
            Return _DataProperty
        End Get
    End Property

    Protected Friend Overrides Sub OnValueChanged([Property] As DashboardZoneProperty)
        If [Property] Is _DataProperty Then
            Dim v = TryCast(_DataProperty.Value, DataView)
            AddHandler v.ListChanged, AddressOf DataViewListChanged
            If v IsNot Nothing Then
                If _DataProperty.DataBinding IsNot Nothing Then
                    Me._Visual.ItemsSource = _DataProperty.Value
                    'Me._Visual.Series(0).DataMapping = "Label=Member;Value=" & "_" & _DataProperty.DataBinding.Aggregate & "_" & _DataProperty.DataBinding.DataMember.Name
                End If
            End If
        End If

        If [Property].PropertyType.IsSubclassOf(GetType(DataGridColumn)) Then

        End If
    End Sub

    Private Sub DataViewListChanged(sender As Object, e As ComponentModel.ListChangedEventArgs)
        '  MsgBox(e.ListChangedType.ToString() & ":" & e.NewIndex & ":" & e.OldIndex & ":" & If(e.PropertyDescriptor IsNot Nothing, e.PropertyDescriptor.Name, "nothing"))
    End Sub

    Protected Friend Overrides Sub OnDeserializeVisual(ByVal visual As System.Windows.UIElement)
        MyBase.OnDeserializeVisual(visual)
        Me._Visual = visual
    End Sub

    Private Sub VisualAutoGeneratedColumns(sender As Object, e As EventArgs)
        Me.Properties.Clear()
        Me.Properties.Add(_DataProperty)
        Me.Properties.Add(Me._GridProperty)
        For Each c In Me._Visual.Columns
            Dim prop = New DashboardZoneProperty(Me, c.Header, c.GetType())
            prop.Value = c
            prop.Group = "Columns"
            Me.Properties.Add(prop)

        Next
    End Sub

    Private Sub VisualAutoGeneratingColumns(sender As Object, e As DataGridAutoGeneratingColumnEventArgs)
        If e.PropertyType.FullName = "System.ComponentModel.IBindingList" Then
            e.Cancel = True
            Return
        End If

        e.Column = New DataGridFormattedTextColumn() With {.Binding = CType(e.Column, DataGridTextColumn).Binding, .Header = e.Column.Header}
    End Sub


   

End Class

Public Class DataGridFormattedTextColumn
    Inherits DataGridTextColumn



    Public Property Format As String
        Get
            Return GetValue(FormatProperty)
        End Get

        Set(ByVal value As String)
            SetValue(FormatProperty, value)
        End Set
    End Property

    Public Shared ReadOnly FormatProperty As DependencyProperty = _
                           DependencyProperty.Register("Format", _
                           GetType(String), GetType(DataGridFormattedTextColumn), _
                           New PropertyMetadata("", AddressOf FormatChanged))

    Private Shared Sub FormatChanged(d As System.Windows.DependencyObject, e As System.Windows.DependencyPropertyChangedEventArgs)
        Dim g As DataGridFormattedTextColumn = d

        If g.Binding IsNot Nothing Then

            g.Binding = New Data.Binding() With {.Path = CType(g.Binding, Data.Binding).Path, .StringFormat = CStr(e.NewValue)}
        End If
    End Sub




End Class